﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.HeatingControlProject._2.数据层;
using thinger.HeatingControlProject._4.实体类;

namespace thinger.HeatingControlProject._1.界面层
{
    public partial class FrmLogin : Form
    {
        #region 无边框拖动 

        private Point mPoint;
        private void Panel_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }

        private void Panel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion
        public FrmLogin()
        {
            InitializeComponent();

        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private SysAdminService adminService = new SysAdminService();
        private void btn_Login_Click(object sender, EventArgs e)
        {
            //验证数据
            if(this.txt_LoginId.Text.Trim().Length == 0 || this.txt_LoginPwd.Text.Trim().Length == 0) 
            {
                MessageBox.Show("用户名或密码不能为空","提示信息");
                return;
            }
            //封装对象
            SysAdmin admin = null;
            try
            {
                admin = new SysAdmin()
                {
                    LoginId = int.Parse(this.txt_LoginId.Text.Trim()),
                    LoginPwd = this.txt_LoginPwd.Text.Trim()
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录账号必须是整数！", "错误提示");
                return;
            }
            //用户查询
            admin = adminService.AdminLogin(admin);

            //判断结果
            if (admin == null)
            {
                MessageBox.Show("登录账号或密码错误！", "登录提示");
                return ;
            }
            else
            {
                Program.currentAdmin = admin;//保存登录用户对象
                this.DialogResult = DialogResult.OK;
            }

        }
    }
}
